package com.changhai.diabetes.action;

import java.util.List;

import com.changhai.diabetes.model.Drug;
import com.changhai.diabetes.model.DrugSuifang;
import com.changhai.diabetes.model.DrugSurvey;
import com.changhai.diabetes.model.DrugTreatment;
import com.changhai.diabetes.model.OpertResultMsg;
import com.changhai.diabetes.service.DrugService;
import com.changhai.diabetes.util.DateConvertor;

/**
 * 药物
 * 
 * @author Andy
 * 
 */
public class DrugAction extends BaseAction {

	private static final long serialVersionUID = 4392936553263004630L;
	private OpertResultMsg opertMsg;
	private Long patientid;
	private List<Drug> drugs;
	private DrugTreatment drugTreatment;
	private DrugSurvey drugSurvey;
	private DrugSuifang drugSuifang;
	private DrugService drugService;
	private String drugtype;
	private String drugname;
	private String drugamount;
	private String drugunit;
	private String drugmethod;
	private String drugfreq;
	private String othername;
	private Integer flag = 0;

	public String execute() throws Exception {
		return super.execute();
	}

	public String query() {
		return "input";
	}

	public String editDrugSurvey() {
		if (patientid != null && patientid > 0) {
			try {
				drugs = drugService.searchDrugList();
				drugSurvey = drugService.searchDrugSurveyByPatientId(patientid);
				tbList = this.getTableForEnter(patientid, flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无药物调查信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditDrugSurvey() {
		if (patientid == null || patientid <= 0 || drugSurvey == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				drugSurvey.setOthername(othername);
				drugSurvey.setDrugtype(drugtype);
				drugSurvey.setDrugname(drugname);
				drugSurvey.setAmount(drugamount);
				drugSurvey.setTakeunit(drugunit);
				drugSurvey.setTakemethod(drugmethod);
				drugSurvey.setTakefreq(drugfreq);
				drugService.saveEditDrugSurvey(drugSurvey);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "药物调查", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editDrugSurvey();
	}

	public String editDrugTreat() {
		if (patientid != null && patientid > 0) {
			try {
				drugs = drugService.searchDrugList();
				drugTreatment = drugService.searchDrugTreatByPatientId(patientid);
				if (drugTreatment != null) {
					if (drugTreatment.getStarttime() == null || drugTreatment.getStarttime().equals("")) {
						drugTreatment.setStarttime(DateConvertor.getCurrentDate("yyyy-MM-dd"));
					}
				}
				tbList = this.getTableForEnter(patientid, flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无药物治疗信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditDrugTreat() {
		if (patientid == null || patientid <= 0 || drugTreatment == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				drugTreatment.setOthername(othername);
				drugTreatment.setDrugtype(drugtype);
				drugTreatment.setDrugname(drugname);
				drugTreatment.setAmount(drugamount);
				drugTreatment.setTakeunit(drugunit);
				drugTreatment.setTakemethod(drugmethod);
				drugTreatment.setTakefreq(drugfreq);
				if (drugTreatment.getStarttime() == null || drugTreatment.getStarttime().equals("")) {
					drugTreatment.setStarttime(DateConvertor.getCurrentDate("yyyy-MM-dd"));
				}
				drugService.saveEditDrugTreat(drugTreatment);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "药物治疗", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editDrugTreat();
	}

	public String editDrugSuifang() {
		if (patientid != null && patientid > 0) {
			try {
				drugs = drugService.searchDrugList();
				drugSuifang = drugService.searchDrugSuifangByPatientId(patientid);
				tbList = this.getTableForEnter(patientid, flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无药物治疗信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditDrugSuifang() {
		if (patientid == null || patientid <= 0 || drugSuifang == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				drugSuifang.setOthername(othername);
				drugSuifang.setDrugtype(drugtype);
				drugSuifang.setDrugname(drugname);
				drugSuifang.setAmount(drugamount);
				drugSuifang.setTakeunit(drugunit);
				drugSuifang.setTakemethod(drugmethod);
				drugSuifang.setTakefreq(drugfreq);
				drugService.saveEditDrugSuifang(drugSuifang);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "药物随访", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editDrugSuifang();
	}
	
	public OpertResultMsg getOpertMsg() {
		return opertMsg;
	}

	public void setOpertMsg(OpertResultMsg opertMsg) {
		this.opertMsg = opertMsg;
	}
	
	public Long getPatientid() {
		return patientid;
	}

	public void setPatientid(Long patientid) {
		this.patientid = patientid;
	}

	public DrugTreatment getDrugTreatment() {
		return drugTreatment;
	}

	public void setDrugTreatment(DrugTreatment drugTreatment) {
		this.drugTreatment = drugTreatment;
	}

	public DrugService getDrugService() {
		return drugService;
	}

	public void setDrugService(DrugService drugService) {
		this.drugService = drugService;
	}

	public List<Drug> getDrugs() {
		return drugs;
	}

	public void setDrugs(List<Drug> drugs) {
		this.drugs = drugs;
	}

	public String getDrugtype() {
		return drugtype;
	}

	public void setDrugtype(String drugtype) {
		this.drugtype = drugtype;
	}

	public String getDrugname() {
		return drugname;
	}

	public void setDrugname(String drugname) {
		this.drugname = drugname;
	}

	public String getDrugamount() {
		return drugamount;
	}

	public void setDrugamount(String drugamount) {
		this.drugamount = drugamount;
	}

	public String getDrugunit() {
		return drugunit;
	}

	public void setDrugunit(String drugunit) {
		this.drugunit = drugunit;
	}

	public String getDrugmethod() {
		return drugmethod;
	}

	public void setDrugmethod(String drugmethod) {
		this.drugmethod = drugmethod;
	}

	public String getDrugfreq() {
		return drugfreq;
	}

	public void setDrugfreq(String drugfreq) {
		this.drugfreq = drugfreq;
	}

	public String getOthername() {
		return othername;
	}

	public void setOthername(String othername) {
		this.othername = othername;
	}

	public DrugSurvey getDrugSurvey() {
		return drugSurvey;
	}

	public void setDrugSurvey(DrugSurvey drugSurvey) {
		this.drugSurvey = drugSurvey;
	}

	public DrugSuifang getDrugSuifang() {
		return drugSuifang;
	}

	public void setDrugSuifang(DrugSuifang drugSuifang) {
		this.drugSuifang = drugSuifang;
	}

	public Integer getFlag() {
		return flag;
	}

	public void setFlag(Integer flag) {
		this.flag = flag;
	}

}
